﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ColorChange : MonoBehaviour
{
    public Button btn1;
    public Button btn2;

    public Color blueColor;
    public Color redColor;
    public Color whiteColor;

    public static int color; //0 default, 1 blue, 2red

    public static ColorChange instance;

    Coroutine  coroutine;

    private void Awake()
    {
        color = 0;
        SetBtnColor();

        btn1.onClick.AddListener(OnClickBtn1);
        btn2.onClick.AddListener(OnClickBtn2);

        instance = this;
    }

    void OnClickBtn1()
    {
        if(coroutine!=null)
        {
            StopCoroutine(coroutine);
        }
        coroutine = StartCoroutine(ClickBtn1());
    }

    void OnClickBtn2()
    {
        if (coroutine != null)
        {
            StopCoroutine(coroutine);
        }
        coroutine = StartCoroutine(ClickBtn2());
    }

    IEnumerator ClickBtn1()
    {
        while (LevelManager.instance.cameraMoving)
        {
            yield return null;
        }

        if (color != 1)
        {
            color = 1;
            MessageManager.Run(MsgKey.ShowBlueBlock);
        }
        else
        {
            color = 0;
            MessageManager.Run(MsgKey.ShowDefaultBlock);
        }
        coroutine = null;
    }

    IEnumerator ClickBtn2()
    {
        while(LevelManager.instance.cameraMoving)
        {
            yield return null;
        }

        if (color != 2)
        {
            color = 2;
            MessageManager.Run(MsgKey.ShowGreenBlock);
        }
        else
        {
            color = 0;
            MessageManager.Run(MsgKey.ShowDefaultBlock);
        }
        coroutine = null;
    }

    void SetBtnColor()
    {
        if (color == 0)
        {
            btn1.image.color = blueColor;
            btn2.image.color = redColor;
        }
        else if(color == 1)
        {
            btn1.image.color = whiteColor;
            btn2.image.color = redColor;
        }
        else if(color == 2)
        {
            btn1.image.color = blueColor;
            btn2.image.color = whiteColor;
        }
    }

    public void SetBtnEnable(bool b)
    {
        btn1.enabled = b;
        btn2.enabled = b;
        print(b);
        if(b)
        {
            SetBtnColor();
        }
        else
        {
            btn1.image.color = Color.gray;
            btn2.image.color = Color.gray;
        }
    }
}
